<template>
  <div class="app-container">
    <el-form class="base-search-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="所属公司" prop="companyId">
        <el-select v-model="queryParams.companyId" placeholder="请选择所属公司" clearable>
          <el-option
            v-for="obj in list"
            :key="obj.id"
            :label="obj.name"
            :value="obj.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="分公司名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入分公司名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="状态" clearable>
          <el-option
            v-for="dict in sys_company_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="base-container mb-50px">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['company:company:add']"
          >新增</el-button>
        </el-col>
        <!-- <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['company:company:edit']"
          >修改</el-button>
        </el-col> -->
        <!-- <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['company:company:remove']"
          >删除</el-button>
        </el-col> -->
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['company:company:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table border v-loading="loading" :data="companyList" @selection-change="handleSelectionChange">
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column label="分公司名称" prop="name" min-width="120" show-overflow-tooltip />
        <el-table-column label="所属公司" prop="superiorName" min-width="120" show-overflow-tooltip />
        <!-- <el-table-column label="分公司负责人" align="center" prop="head" min-width="120" show-overflow-tooltip /> -->
        <el-table-column label="联系电话" align="center" prop="phone" width="120" />
        <el-table-column label="分公司地址" align="center" prop="address" min-width="150" show-overflow-tooltip />
        <el-table-column label="状态" align="center" width="100">
          <template #default="scope">
            <el-switch
              v-model="scope.row.state"
              active-value="1"
              inactive-value="2"
              inline-prompt
              active-text="启用"
              inactive-text="禁用"
              @change="handleStatusChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="更新时间" align="center" prop="updateTime" width="180" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
          <template #default="scope">
            <el-button link type="primary" icon="View" @click="handleDetail(scope.row)" v-hasPermi="['company:company:query']">详情</el-button>
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['company:company:edit']">修改</el-button>
            <!-- <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['company:company:remove']">删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公司对话框 -->
    <Dialog v-model="showDialog" :title="dialogTitle" width="60%">
      <edit-company 
        v-if="dialogType == 1"
        ref="editCompany"
        :list="list"
        @cancel="cancel"
        @confirm="confirm"
      />
      <company-detail 
        v-if="dialogType == 2"
        ref="companyDetail"
        :list="list"
        @cancel="cancel"
      />
    </Dialog>
  </div>
</template>

<script setup name="Company">
import { listCompany, listSubCompany, delCompany, updateCompany } from "@/api/company/company"
import { nextTick } from "vue"
import EditCompany from './edit.vue'
import CompanyDetail from './detail.vue'

const editCompany = ref()
const companyDetail = ref()
const { proxy } = getCurrentInstance()
const { sys_company_status } = proxy.useDict("sys_company_status")

const list = ref([])
const companyList = ref([])   // 分公司
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const showDialog = ref(false)
const dialogTitle = ref('')
const dialogType = ref(0)     // 1新增修改  2详情 

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    state: null,
    companyId: null
  }
})

const { queryParams } = toRefs(data)

/** 查询一级公司列表 */
const getCompanyList = () => {
  const params = {
    pageNum: 1,
    pageSize: 1000,
  }
  listCompany(params).then(res => {
    list.value = res.rows
  })
}

/** 查询分公司列表 */
function getList() {
  loading.value = true
  const params = {
    ...queryParams.value,
    companyId: queryParams.value.companyId || '0'
  }
  listSubCompany(params).then(response => {
    companyList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  showDialog.value = false
  dialogTitle.value = ''
  dialogType.value = 0
}

// 确认按钮
const confirm = () => {
  cancel()
  getList()
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  showDialog.value = true
  dialogTitle.value = '添加分公司'
  dialogType.value = 1
  nextTick(() => {
    editCompany.value.pageInit()
  })
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _id = row.id || ids.value
  showDialog.value = true
  dialogType.value = 1
  dialogTitle.value = '修改分公司'
  nextTick(() => {
    editCompany.value.pageInit(_id)
  })
}

/** 公司详情 */
function handleDetail(row) {
  dialogTitle.value = '分公司详情'
  dialogType.value = 2
  showDialog.value = true
  nextTick(() => {
    companyDetail.value.pageInit(row.id)
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除该分公司？').then(function() {
    return delCompany(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  const params = {
    ...queryParams.value,
    companyId: queryParams.value.companyId || '0'
  }
  proxy.download('company/company/export/sub', {
    ...params
  }, `分公司列表_${proxy.getFileImportName()}.xlsx`)
}

/** 状态修改 */
function handleStatusChange(row) {
  let text = row.state == 1 ? "启用" : "禁用"
  proxy.$modal.confirm('确认要"' + text + '""' + row.name + '"分公司吗?').then(function () {
    const params = {
      id: row.id,
      state: row.state
    }
    return updateCompany(params)
  }).then(() => {
    proxy.$modal.msgSuccess(text + "成功")
  }).catch(function () {
    row.state = row.state == 1 ? 2 : 1
  })
}

getList()
getCompanyList()
</script>
